import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm'
import { RolePermissions } from './RolePermissions'
import { Systems } from './Systems'
import { Users } from './Users'
import { UserRoles } from './UserRoles'

@Index('fk_role_syetem_id', ['systemId'], {})
@Index('fk_role_user_create_id', ['createBy'], {})
@Index('fk_roue_user_update_id', ['updateBy'], {})
@Index('name', ['name'], { unique: true })
@Index('uniq_name', ['name'], { unique: true })
@Entity('roles', { schema: 'nest-demo' })
export class Roles {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id', unsigned: true })
  id: number

  @Column('varchar', {
    name: 'name',
    unique: true,
    comment: '角色唯一标识符，如 admin, user, editor',
    length: 50,
  })
  name: string

  @Column('tinyint', {
    name: 'status',
    nullable: true,
    comment: '状态: 0-禁用, 1-启用',
    width: 1,
    default: () => "'1'",
  })
  status: boolean | null | number | string

  @Column('varchar', {
    name: 'code',
    nullable: true,
    comment: '角色代码：如：wx-bbs-base、bbs-notify-admin...',
    length: 50,
  })
  code: string | null

  @Column('int', {
    name: 'system_id',
    nullable: true,
    comment: '所属系统ID',
    unsigned: true,
  })
  systemId: number | null

  @Column('enum', {
    name: 'type',
    nullable: true,
    comment: '角色类型',
    enum: ['system', 'customSys'],
    // default: () => "'customsys'",
    default: 'customSys',
  })
  type: 'system' | 'customSys' | null

  @Column('varchar', {
    name: 'grace',
    nullable: true,
    comment:
      '角色等级：如：NS_SUPER_ADMIN_ROLE 超级管理员角色，NS_BASE_ROLE 普通用户 ,NS_ORDINARY_ADMIN_ROLE 普通管理员角色',
    length: 50,
  })
  grace: string | null

  @Column('varchar', {
    name: 'description',
    nullable: true,
    comment: '角色描述',
    length: 255,
  })
  description: string | null

  @Column('bigint', {
    name: 'create_by',
    nullable: true,
    comment: '创建者ID',
    unsigned: true,
  })
  createBy: string | null

  @Column('bigint', {
    name: 'update_by',
    nullable: true,
    comment: '最后更新者ID',
    unsigned: true,
  })
  updateBy: string | null

  @Column('timestamp', {
    name: 'created_at',
    nullable: true,
    default: () => 'CURRENT_TIMESTAMP',
  })
  createdAt: Date | null

  @Column('timestamp', {
    name: 'updated_at',
    nullable: true,
    default: () => 'CURRENT_TIMESTAMP',
  })
  updatedAt: Date | null

  @OneToMany(() => RolePermissions, (rolePermissions) => rolePermissions.role)
  rolePermissions: RolePermissions[]

  @ManyToOne(() => Systems, (systems) => systems.roles, {
    onDelete: 'CASCADE',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'system_id', referencedColumnName: 'id' }])
  system: Systems

  @ManyToOne(() => Users, (users) => users.roles, {
    onDelete: 'CASCADE',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'create_by', referencedColumnName: 'id' }])
  createBy2: Users

  @ManyToOne(() => Users, (users) => users.roles2, {
    onDelete: 'CASCADE',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'update_by', referencedColumnName: 'id' }])
  updateBy2: Users

  @OneToMany(() => UserRoles, (userRole) => userRole.role)
  userRoles: UserRoles[]
}
